<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Appeal;
use App\Models\Message;
use App\Models\Recharge;
use Cloud;
use DB;
use Exception;

class AppealController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'appeal');
    }

    /**
     * 申诉列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function appeal_list(Request $request)
    {
        $info = $request->all();
        $data = array();
        $order_no = '';
        if(!empty($info['order_no'])){
            $order_no = $info['order_no'];
            $where['order_no'] = $info['order_no'];
            $rid = Recharge::where($where)->first();
            if ($rid)
                $data = Appeal::where('rid',$rid->id)->orderBy('created_at', 'desc')->paginate(8);
        }else{
            $data = Appeal::orderBy('created_at', 'desc')->paginate(8);
        }
        $page_num = $data->firstItem();

        return view('admin/appeal/appeal_list', compact('data', 'page_num','order_no'));
    }

    /**
     * 申述详情
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function appeal_info(Request $request)
    {
        $id = $request->get('id');
        $data = Appeal::find($id);

        return view('admin/appeal/appeal_info', compact('data'));
    }

    /**
     * 保存仲裁结果
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function save_arbitration(Request $request)
    {
        //申述单ID
        $appeal_id = $request->get('appeal_id');
        //申述状态
        $status = $request->get('status');
        //备注
        $note = $request->get('note');
        //没有选择仲裁
        if ($status == 0) {
            return back()->with(['status' => 0, 'msg' => '请选择一个结果']);
        }
        //申述订单
        $appeal = Appeal::find($appeal_id);
        //交易单
        $rech = Recharge::find($appeal->rid);
        //订单必须是申述中状态
        if ($rech->status != 5) {
            return back()->with(['status' => 0, 'msg' => '该订单不是申诉中订单']);
        }

        //判断申述方胜出
        if ($status == 2) {
            //申述方成功
            $appeal->update(['status' => 2]);
            //被申述方失败
            $appeal->appeal_reply()->update(['status' => 2, 'note' => $note]);
            //如果申述人是用户
            if ($appeal->user->type == 1) {
                //写入消息（申述成功）
                $msg = ['msg_id' => 20, 'uid' => $rech->uid, 'message' => [$rech->order_no],"arr"=> ['number' => $rech->id]];
                (new Message())->syspublishsmsAll($msg);
            }
            //备份状态
            $backup_status = $rech->backup_status;
            //回滚状态
            $rech->update(['status' => $backup_status]);
        }   
        //判定被申述方胜出
        if ($status == 3) {
            //申述方失败
            $appeal->update(['status' => 3, 'note' => $note]);
            //被申述方成功
            $appeal->appeal_reply()->update(['status' => 1]);
            //如果申述人是用户
            if ($appeal->user->type == 1) {
                //写入消息(申述被驳回)
                $msg = ['msg_id' => 21, 'uid' => $rech->uid, 'message' => [$rech->order_no],"arr"=> ['number' => $rech->id]];
                (new Message())->syspublishsmsAll($msg);
            }
            //备份状态
            $backup_status = $rech->backup_status;
            if ($backup_status == 5) {
            	$backup_status = 2;
            }
            //回滚状态
            $rech->update(['status' => $backup_status]);
        }
        //修改订单状态
        //$rech->update(['status' => 6]);

        return redirect('admin/appeal_list');
    }
}
